package com.aramco.fion.controller
{
	import com.aramco.fion.event.BaseEvent;
	import com.aramco.fion.event.LoadQueryEvent;
	import com.aramco.fion.model.AppModel;
	import com.aramco.fion.model.FlexConstants;
	import com.aramco.fion.model.MyXML;
	import com.aramco.fion.model.QueryModel;
	import com.aramco.fion.service.AppDelegate;
	import com.aramco.fion.service.IAppDelegate;
	import com.aramco.fion.service.LoadQueryDelegate;
	import com.aramco.fion.service.UserService;
	import com.aramco.fion.util.AppUtil;
	import com.aramco.fion.util.BSPReportDataModelOO;
	import com.aramco.fion.util.ErrorUtil;
	import com.aramco.fion.util.QueryUtil;
	import com.aramco.fion.util.XmlTreeUtil;
	import com.aramco.fion.vo.F4Value;
	
	import ext.swizframework.utils.AsyncInterceptor;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.events.ItemClickEvent;
	import mx.logging.ILogger;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import org.swizframework.utils.services.ServiceHelper;


	public class QueryController
	{
		
		[Inject]
		public var queryModel:QueryModel;
		
		[Inject]
		public var baseController:BaseController;
		
		[Inject]
		public var delegate:LoadQueryDelegate;
		
		[Inject]
		public var serviceRequestUtil:ServiceHelper;
		
		[Log]			
		public var log  : ILogger = null;	
		
		private var currentEvent:Event;

		/**
		 * Swiz will automatically create any Bean for of the built-in helper classes
		 * if one has not been defined.
		 */ 
		//public var serviceHelper : ServiceHelper;
		
		public function QueryController()
		{
			
		}
		
		//---------------- Load F4 Variables ---------------------------------

		[EventHandler(event = "LoadQueryEvent.LOAD_XML_F4_EVENT")]
		public function loadF4Help(event:LoadQueryEvent):void
		{
			currentEvent  = event;
			
			if(event.parm.iobjnm == "0COSTCENTER")
			{
				serviceRequestUtil.executeServiceCall( delegate.loadF4CostCenter(event.parm), loadResultHandler, loadFaulttHandler );
			}
			else if(event.parm.iobjnm == "0ORGUNIT")
			{
				serviceRequestUtil.executeServiceCall( delegate.loadF4Org(event.parm), loadResultHandler, loadFaulttHandler );
			}
			else
			{
				var call:AsyncToken = delegate.loadF4TRS(event.parm);
				var agent : AsyncInterceptor = new AsyncInterceptor(QueryUtil.getF4Values);
				serviceRequestUtil.executeServiceCall( agent.intercept(call), loadResultHandler, loadFaulttHandler );
				
			}
		}

		//---------------- Load APP XML Structure ---------------------------------
		[EventHandler(event = "LoadQueryEvent.LOAD_XML_APP_EVENT")]
		public function loadAppHelp(event:LoadQueryEvent):void
		{
			baseController.cacheCaller(event);
			currentEvent  = event;
			var call:AsyncToken = delegate.loadAppXml('app6.xml');
			var agent : AsyncInterceptor = new AsyncInterceptor(XmlTreeUtil.parseAppXML);
			serviceRequestUtil.executeServiceCall( agent.intercept(call), loadResultHandler, loadFaulttHandler );
		}
		
		//---------------- Load Selection Screen ---------------------------------
		[EventHandler(event = "LoadQueryEvent.LOAD_XML_QUERY_SELECTION_EVENT")]
		public function loadQuerySelection(event:LoadQueryEvent):void
		{
			baseController.cacheCaller(event);
			currentEvent  = event;
			var agent : AsyncInterceptor = new AsyncInterceptor(QueryUtil.getVariablesScreenMetadata);
			var call:AsyncToken = delegate.loadSelection(event.queryName);
			serviceRequestUtil.executeServiceCall( agent.intercept(call), loadResultHandler, loadFaulttHandler );
		}
		
		//---------------- Load XML REPORT ---------------------------------
		[EventHandler(event = "LoadQueryEvent.LOAD_XML_REPORT_EVENT")]
		public function loadXmlReport(event:LoadQueryEvent):void
		{
			baseController.cacheCaller(event);
			currentEvent = event;
			var agent : AsyncInterceptor = new AsyncInterceptor(QueryUtil.getXMLReport);
			var call:AsyncToken = delegate.loadReportByQueryName(event.queryName);
			serviceRequestUtil.executeServiceCall( agent.intercept(call), loadResultHandler, loadFaulttHandler );
		}
		

		//---------------- Load APP XML Structure ---------------------------------
		[EventHandler(event = "LoadQueryEvent.LOAD_WELCOME_PAGE_EVENT")]
		public function loadWelcomePage(event:LoadQueryEvent):void
		{
			baseController.cacheCaller(event);
			currentEvent  = event;
			var call:AsyncToken = delegate.loadWelcomePage(FlexConstants.WELCOME_PAGE_URL);
			serviceRequestUtil.executeServiceCall( call, loadResultHandler, loadFaulttHandler );
			
		}
		
		
		//------------ Handel Result ---------------------------------
		private function loadResultHandler(data: * ):void 
		{
			if(currentEvent.type == LoadQueryEvent.LOAD_XML_APP_EVENT)
			{
				if (log != null) log.debug("loadResultHandler(data: * )");
			//	trace(data);
				queryModel.appMeta = data;
				baseController.notifyCaller(data);
			}
			
			else if(currentEvent.type == LoadQueryEvent.LOAD_XML_QUERY_SELECTION_EVENT)
			{
				trace(data);
				queryModel.querySelectionsMeta = data;
				baseController.notifyCaller(data);
			}
			
			else if(currentEvent.type == LoadQueryEvent.LOAD_XML_F4_EVENT)
			{
				if(data is ArrayCollection)
				{
					queryModel.f4Data = data;
				}
				else
				{
					queryModel.f4Xml = data.result.root.ccs;
					baseController.notifyCaller(data);
				}
			}
			
			else if(currentEvent.type == LoadQueryEvent.LOAD_XML_REPORT_EVENT)
			{
			//	var myXML:XML = data.result as XML;
			//	queryModel.queryOutput = new BSPReportDataModelOO(myXML);
				queryModel.queryOutput = data;
				baseController.notifyCaller(data);
			}
			
			else if(currentEvent.type == LoadQueryEvent.LOAD_WELCOME_PAGE_EVENT)
			{
				//	var myXML:XML = data.result as XML;
				//	queryModel.queryOutput = new BSPReportDataModelOO(myXML);
				baseController.notifyCaller(data);
			}
		}
		
		//------------ Handel Fault ---------------------------------
		private function loadFaulttHandler(event:FaultEvent):void
		{
			trace(event);
		}
	}
}